package com.onlineauction.entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.onlineauction.helpers.Constants;

public class Review {
	private Review(String reviewer, String reviewee, int rating, String comment,
			Date timestamp, String title) {
		super();
		this.reviewer = reviewer;
		this.reviewee = reviewee;
		this.rating = rating;
		this.comment = comment;
		this.timestamp = timestamp;
		this.title = title;
	}
	public Review(){}
	protected String reviewer;
	protected String reviewee;
	protected int rating;
	protected String comment;
	protected Date timestamp;
	protected String title;
	
	private static Review fromEntity(Entity entity){
		Review review = new Review();
		
		review.setComment((String) entity.getProperty(Constants.COMMENT));
		review.setRating((Long) entity.getProperty(Constants.RATING));
		review.setReviewer((String) entity.getProperty(Constants.REVIEWER));
		review.setReviewee((String) entity.getProperty(Constants.REVIEWEE));
		review.setTimestamp((Date) entity.getProperty(Constants.TIMESTAMP));
		review.setTitle((String) entity.getProperty(Constants.TITLE));
		
		return review;
	}
	public static Review createReview(String reviewer, String reviewee, int rating, String comment,
			Date timestamp, String title, boolean isStore){
		Review review = new Review(reviewer, reviewee, rating, comment, timestamp, title);
		if(!isStore){
			return review;
		}
		Entity entity = new Entity(Constants.REVIEW);
		entity.setProperty(Constants.TITLE, title);
		entity.setProperty(Constants.REVIEWER, reviewer);
		entity.setProperty(Constants.REVIEWEE, reviewee);
		entity.setProperty(Constants.COMMENT, comment);
		entity.setProperty(Constants.RATING, rating);
		entity.setProperty(Constants.TIMESTAMP, timestamp);
		
		if(!GenericEntity.saveEntity(entity)){
			review = null;
		}
		
		return review;
	}
	
	public static List<Review> getReviewsForUser(String revieweeName){
		
		List<Review> reviews = new ArrayList<Review>();
		
		// Get the Datastore Service
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Filter filter =   new FilterPredicate(Constants.REVIEWEE, FilterOperator.EQUAL,revieweeName);
		
		Query q = new Query(Constants.REVIEW).setFilter(filter);
		q.addSort(Constants.TIMESTAMP, SortDirection.DESCENDING);
		
		PreparedQuery pq = datastore.prepare(q);
		for (Entity result : pq.asIterable()) {
		  reviews.add(fromEntity(result));
		}
		
		return reviews;
	}
	
	public String getReviewer() {
		return reviewer;
	}
	public void setReviewer(String reviewer) {
		this.reviewer = reviewer;
	}
	public String getReviewee() {
		return reviewee;
	}
	public void setReviewee(String reviewee) {
		this.reviewee = reviewee;
	}
	public int getRating() {
		return rating;
	}
	public void setRating(long rating) {
		this.rating = (int)rating;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public Date getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

}
